{extend name="layout" /}
{block name="body"}
<div class="row">
    <div class="col-lg-12">
        <div class="card">
            <div class="card-toolbar d-flex flex-column flex-md-row">
                <div class="toolbar-btn-action">
                    <a class="btn btn-primary m-r-5" data-toggle="modal" data-target="#exampleModal"><i
                            class="mdi mdi-plus"></i> 新增</a>
                </div>
            </div>
            <div class="card-body">
                <div class="table-responsive">
                    <table class="table table-bordered">
                        <thead>
                            <tr>
                                <th>-</th>
                                <th>消息标题</th>
                                <th>消息内容</th>
                                <th>作者</th>
                                <th>app名字</th>
                                <th>发送时间</th>
                                <th>操作</th>
                            </tr>
                        </thead>
                        <tbody>
                            {foreach $data as $key=>$vo}
                            <tr>
                                <td>{$vo.id}</td>
                                <td>{$vo.title}</td>
                                <td>{$vo.content}</td>
                                <td>管理员</td>
                                <td>{$vo.appname}</td>
                                <td>{$vo.time}</td>
                                <td>
                                    <div class="btn-group">
                                        <a class="btn btn-xs btn-default ajax-get confirm" title=""
                                            data-toggle="tooltip" data-original-title="删除"
                                            onclick="deletemsg({$vo.id})"><i class="mdi mdi-window-close"></i></a>
                                    </div>
                                </td>
                            </tr>
                            {/foreach}
                        </tbody>
                    </table>
                </div>
                {$data|raw}
            </div>
        </div>
    </div>
</div>
<div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel"
    aria-hidden="true">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <h6 class="modal-title" id="exampleModalLabel">新增消息通知</h6>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
            </div>
            <div class="modal-body">
                <div class="form-group">
                    <label for="title">消息标题</label>
                    <input type="text" class="form-control" id="title" placeholder="请输入消息标题">
                </div>
                <div class="form-group">
                    <label for="content">消息内容</label>
                    <textarea class="form-control" id="content" rows="3" placeholder="消息内容"></textarea>
                </div>
                <div class="form-group">
                    <label for="appid">appid</label>
                    <select class="form-control" id="appid">
                        <option value="" disabled>请选择app</option>
                        {foreach :query('app') as $key=>$value }
                        <option value="{$value['appid']}">{$value["appname"]}(appid:{$value['appid']})</option>
                        {/foreach}
                    </select>
                </div>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" id="shutdown" data-dismiss="modal">关闭</button>
                <button type="button" id="add" class="btn btn-w-sm btn-primary">保存</button>
            </div>
        </div>
    </div>
</div>
{/block}
{block name="js"}
<script>
    $("#add").click(function () {
        var l = $('#add').lyearloading({
            opacity: 0.2,
            spinnerSize: 'nm'
        });
        var title = $.trim($("#title").val());
        var content = $.trim($("#content").val());
        var appid = $.trim($("#appid").val());
        if (title == "" || content == "") {
            notify.error("请输入完整！");
            return false;
        }
        $.ajax({
            type: 'post',
            url: "{:url('app/addmessages')}",
            data: {
                'title': title,
                'content': content,
                'appid': appid
            },
            dataType: "json",
            success: function (data) {
                l.destroy();
                if (data.code == 1) {
                    notify.success(data.msg, function () {
                        location.reload();
                    }, 1000);
                } else {
                    notify.error(data.msg);
                    $("#exampleModal").modal('hide');
                }
            },
            error: function () {
                l.destroy();
                notify.error("系统错误");
            }
        })
    });

    function deletemsg(id) {
        $.confirm({
            title: '确认删除',
            content: "确定删除此通知？",
            autoClose: 'cancelAction|10000',
            escapeKey: 'cancelAction',
            buttons: {
                confirm: {
                    btnClass: 'btn-red',
                    text: '确定删除通知',
                    action: function () {
                        var l = $('body').lyearloading({
                            opacity: 0.2,
                            spinnerSize: 'lg',
                            spinnerText: '后台处理中，请稍后...',
                            textColorClass: 'text-info',
                            spinnerColorClass: 'text-info'
                        });
                        $.ajax({
                            type: "POST",
                            url: "{:url('app/deletemsg')}",
                            data: {
                                id: id,
                            },
                            dataType: "json",
                            success: function (data) {
                                l.destroy();
                                if (data.code == 1) {
                                    notify.success(data.msg, function () {
                                        location.reload();
                                    }, 1000);
                                } else {
                                    notify.error(data.msg);
                                    $("#exampleModal").modal('hide');
                                }
                            },
                            error: function () {
                                l.destroy();
                                notify.error("系统错误");
                            }
                        })

                    }
                },
                cancelAction: {
                    text: '取消',
                    action: function () {
                    }
                }
            }
        });
    }

</script>
{/block}